package com.example.demo.controller;
import com.example.demo.entity.File;
import com.example.demo.exception.file.InvalidPathException;
import com.example.demo.service.SearchService;
import com.example.demo.utils.Message;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("search")
public class SearchController {
    @Resource
    SearchService searchService;
    /**
     * 获取分享链接
     * @param path 被分享的文件路径
     * @return allPath 分享链接
     */
    @RequestMapping(value="shareFile",method = RequestMethod.GET)
    public Message shareFile(@RequestParam("path") String path){
        String msg="原路径有误，无法分享链接";
        try{
            String allPath=searchService.shareFile(path);
            return Message.createSuc(allPath);
        } catch (InvalidPathException e) {
            e.printStackTrace();
            return Message.createErr(msg);
        }
    }

    //普通搜索
    @RequestMapping(value="searchFile",method = RequestMethod.GET)
    public Message searchFile(@RequestParam(value = "orderName",required = false) String orderName,@RequestParam(value = "ascending",required = false) Boolean ascending,@RequestParam(value = "name",required = false) String name,@RequestParam(value = "be_star",required = false) Boolean be_star,@RequestParam(value = "be_share",required = false) Boolean be_share,@RequestParam(value = "be_new",required = false) Boolean be_new,@RequestParam(value = "keyWords",required = false) String keyWords){
        try {
            List<File> searchFile = searchService.searchFile(orderName,ascending,name,be_star,be_share,be_new,keyWords);
            return Message.createSuc(searchFile);
        }catch (Exception e) {
            String msg;
            e.printStackTrace();
            msg="搜索失败。";
            return Message.createErr(msg);
        }
    }

    /**
     * 搜索 给定文件路径的下一级文件/夹
     * @param orderName 排序的名称
     * @param ascending 升降序
     * @param filePath 搜索的路径
     * @param keyWords 关键词
     * @return
     */
    @RequestMapping(value="searchNextFile",method = RequestMethod.GET)
    public Message searchFile(@RequestParam(value = "orderName",required = false) String orderName,@RequestParam(value = "ascending",required = false) Boolean ascending,@RequestParam(value = "filePath",required = false) String filePath,@RequestParam(value = "keyWords",required = false) String keyWords){
        try {
            Object searchFile = searchService.searchNextFile(orderName,ascending,filePath,keyWords);
            return Message.createSuc(searchFile);
        }catch (Exception e) {
            String msg;
            e.printStackTrace();
            msg="搜索失败。";
            return Message.createErr(msg);
        }
    }

    /**
     * 搜索回收站 给定回收站的下一级文件/夹
     * @param orderName 排序的名称
     * @param ascending 升降序
     * @param be_deleted 回收站1
     * @param keyWords 关键词
     * @return
     */
    @RequestMapping(value="searchDeletedFile",method = RequestMethod.GET)
    public Message searchDeletedFile(@RequestParam(value = "orderName",required = false) String orderName,@RequestParam(value = "ascending",required = false) Boolean ascending,@RequestParam(value = "be_deleted",required = false) Boolean be_deleted,@RequestParam(value = "keyWords",required = false) String keyWords){
        try {
            Object searchFile = searchService.searchDeletedFile(orderName,ascending,be_deleted,keyWords);
            return Message.createSuc(searchFile);
        }catch (Exception e) {
            String msg;
            e.printStackTrace();
            msg="搜索失败。";
            return Message.createErr(msg);
        }
    }
}
